
#include <stdio.h>
#include "itcpclient.h"
#include "tools.h"
#define MAX_CLIENT 10

class ClientNetworkDelegate : public NetworkDelegate
{
public:
	virtual int CloseConnectCallBack(ItcpThreadOwner *owner, int connectIdx)
	{
		RETURN( errcode::enFunctionReturnValue_Sucess);
	}
	virtual int OpenConnectCallBack(ItcpThreadOwner *owner, int connectIdx)
	{
		RETURN( errcode::enFunctionReturnValue_Sucess);
	}
};

int main(int argc, char *argv[])
{

	//virtual int StartClient( char* serverIP,  PORT listenPort, int maxCon = MIN_CONN ) = 0;
	//virtual int StopClient( ) = 0;
	//virtual int ActionClient() = 0;

	//virtual int ConnectToServer( ) = 0;
	//virtual int DisConnectFromServer(  int ConntedIdx  ) = 0;
	if(argc < 3)
	{
		OutPutDebugStringToCMD("not enough param!\n");
		return 1;
	}


	OutPutDebugStringToCMD("ip:%s port:%s!\n", argv[1], argv[2]);

	Sleep(1000);
	ITCPClient *Client[MAX_CLIENT];
	char *data = "data 001-002\n";
	char recvRet[50] = {0};
	TCPNetPack *pack = (TCPNetPack*)recvRet;
	strcpy(pack->buff, data);
	ClientNetworkDelegate _delegate;
	TimerStart();
	for (int i = 0; i < MAX_CLIENT; i++)
	{
		Client[i] = CreateTCPClient();
		if ( !Client[i] )
			return 1;

		
		Client[i]->StartClient(argv[1], atoi(argv[2]));

		//Client[i]->SetNetworkDelegate(&_delegate);

		if(Client[i]->ConnectToServer() != 0)
		{
			OutPutDebugStringToCMD("can't connect server!");
		}


		Sleep(1);
		//OutPutDebugStringToCMD(pack->buff);
		//Client->PushDataToSendBuff(pack->ConnectionID,  recvRet, strlen(recvRet));

	}

	while (true)
	{
		
		for (int i = 0; i < MAX_CLIENT; i++)
		{
			sprintf(recvRet,"data____________%04d\n", i);
			Client[i]->PushDataToSendBuff(0, recvRet, strlen(recvRet));

			do
			{

				void *header = Client[i]->Peek(0, sizeof(TCPNetPack));
				if (header == NULL)
				{
					break;
				}

				TCPNetPack *PackHeader = (TCPNetPack*)header;
				if (PackHeader->buffsize <= 0)
				{
					getchar();
					return 1;
				}
				memset(recvRet, 0 , 50);
				Client[i]->GetDataFromRecvBuffByLength(0, recvRet, sizeof(TCPNetPack)+PackHeader->buffsize-1);
				if (recvRet == NULL)
				{
					break;
				}

				TCPNetPack *Packet = (TCPNetPack*)recvRet;

				OutPutDebugStringToCMD("Recv:%d %s",Packet->buffsize, Packet->buff);

			}while(0);

			Sleep(50);
		}

	}

	getchar();

	for (int i = 0; i < MAX_CLIENT; i++)
	{
		Client[i]->StopClient();
	}
	return 0;
}
